<template>
  <view class="st-u-comment-hierarchical-for-hierarchical-publish">
    <view class="st-flex-align-between" style="padding-top: 30rpx;padding-bottom: 40rpx;">
      <u-button size="mini" @click="saveLocal">保存草稿</u-button>
      <u-button type="success" size="mini" @click="reply" >确认发送</u-button>
    </view>
    <u-field
        v-model="content.content"
        type="textarea"
        :placeholder="contentPh"
        :clearable="false"
        :label-width="0"
        :focus="true"
    ></u-field>
  </view>

</template>

<script>
// 该组件用于添加评论、回复评论等
import CommentHierarchicalUrl from '../../../../pc/hierarchical/CommentHierarchicalUrl.js'
export default {
  props: {
    // 输入提示
    contentPh:{
      type: String,
      default: '说点什么...'
    },
    /**
     * 分组标识
     */
    groupFlag: {
      type: String
    },
    // 主体 id，如动态 id
    commentHierarchicalId: {
      type: String,
      required: true,
    },
    targetUserId:{
      type: String
    }
  },
  data(){
    return {
      replyLoading: false,
      content: {
        content: ''
      },

    }
  },
  mounted(){
    let saveLocal = uni.getStorageSync('commentHierarchicalForHierarchicalPublishLocal')
    if(saveLocal){
      this.content = saveLocal
    }
  },
  methods: {
    // 保存草稿
    saveLocal(){
      if(!this.content.content && urls.length == 0){
        this.$u.toast('该写点什么呢')
        return
      }
      let data = {
        content: this.content.content,
      }
      uni.setStorageSync('commentHierarchicalForHierarchicalPublishLocal', data)
      this.$u.toast('保存成功')
      this.$emit('saveSuccess')
    },
    // 发送
    reply(){

      if(!this.content.content){
        this.$u.toast('该写点什么呢')
        return
      }
      let data = {
        content: this.content.content,
        commentHierarchicalId: this.commentHierarchicalId,
        targetUserId: this.targetUserId,
        groupFlag: this.groupFlag
      }

      uni.showLoading({
        title: '发送中'
      });
      this.$http.post(CommentHierarchicalUrl.addCommentHierarchicalForHierarchical,data).then(res=>{
        this.$u.toast('发送成功')
        // 异步清除草稿
        uni.removeStorage({key: 'commentHierarchicalForHierarchicalPublishLocal'})
        this.$emit('publishSuccess',res.data)
      }).catch(error=>{
        this.$u.toast('发送失败，请稍候再试')
      }).finally(() => {
        uni.hideLoading();
      })
    }
  }
}
</script>

<style scoped>

</style>